#if !defined(AFX_CSSFORMAT_H__20000615_9BFD_0BB5_6F1A_0080AD509054__INCLUDED_)
#define AFX_CSSFORMAT_H__20000615_9BFD_0BB5_6F1A_0080AD509054__INCLUDED_
//
// Written by Bjarke Viksoe (bjarke@viksoe.dk)
// Copyright (c) 2000.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is 
// not sold for profit without the authors written consent, and 
// providing that this notice and the authors name is included. 
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever. It's free, so don't hassle me about it.
//
// Beware of bugs.
//

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CCssFormat
{
public:
   static CString Format(LPCTSTR pstrText)
   {
      ATLASSERT(pstrText);
      if( pstrText==NULL ) return _T("");

      enum STATE { 
         STATE_DEFINITION, 
         STATE_TAG, 
         STATE_STRING, 
         STATE_COMMENT 
      } State;

      CString s;
      DWORD dwSize = _tcslen(pstrText)*8;
      if( dwSize<4000 ) dwSize=8000;
      LPTSTR pstrTarget = s.GetBuffer(dwSize);
      LPTSTR pEnd = pstrTarget + dwSize - 256;
      LPTSTR pT = pstrTarget;

      State = STATE_DEFINITION;
      *pT++ = _T('\\');
      *pT++ = _T('b');
      *pT++ = _T(' ');

      TCHAR ch;
      while( (ch = *pstrText++)!=_T('\0') ) {
         switch( ch ) {
         case _T('{'):
            if( State == STATE_DEFINITION ) {
               State = STATE_TAG;
               *pT++ = _T('\\');
               *pT++ = _T('b');
               *pT++ = _T('0');
               *pT++ = _T(' ');
               *pT++ = _T('\\');
               *pT++ = _T('{');
               *pT++ = _T('\\');
               *pT++ = _T('c');
               *pT++ = _T('f');
               *pT++ = _T('1');
               *pT++ = _T(' ');
            }
            else {
               *pT++ = ch;
            }
            break;
         case _T('}'):
            if( State==STATE_TAG ) {
               State = STATE_DEFINITION;
               *pT++ = _T('\\');
               *pT++ = _T('c');
               *pT++ = _T('f');
               *pT++ = _T('0');
               *pT++ = _T(' ');
               *pT++ = _T('\\');
               *pT++ = _T('}');
               *pT++ = _T('\\');
               *pT++ = _T('b');
               *pT++ = _T('1');
               *pT++ = _T(' ');
            }
            else {
               *pT++ = ch;
            }
            break;
         case _T('\"'):
            if( State == STATE_TAG ) {
               State = STATE_STRING;
               *pT++ = ch;
               *pT++ = _T('\\');
               *pT++ = _T('I');
               *pT++ = _T(' ');
            }
            else if( State == STATE_STRING ) {
               State = STATE_TAG;
               *pT++ = _T('\\');
               *pT++ = _T('I');
               *pT++ = _T('0');
               *pT++ = _T(' ');
               *pT++ = ch;
            }
            else {
               *pT++ = ch;
            }
            break;
         case _T('\\'):
            *pT++ = _T('\\');
            *pT++ = _T('\\');
            break;
         case _T('\r'):
            break;
         case _T(';'):
            if( State == STATE_TAG ) {
               *pT++ = _T('\\');
               *pT++ = _T('c');
               *pT++ = _T('f');
               *pT++ = _T('1');
               *pT++ = _T(' ');
               // fall through...
            }
            *pT++ = ch;
            break;
         case _T(':'):
            if( State == STATE_TAG ) {
               *pT++ = _T('\\');
               *pT++ = _T('c');
               *pT++ = _T('f');
               *pT++ = _T('0');
               *pT++ = _T(' ');
               // fall through...
            }
            *pT++ = ch;
            break;
         case _T('\n'):
            *pT++ = _T('\\');
            *pT++ = _T('p');
            *pT++ = _T('a');
            *pT++ = _T('r');
            *pT++ = _T(' ');
            break;
         default:
            *pT++ = ch;
         }

         if( pT > pEnd ) break; // TODO: Add intelligent string growth
      };
      *pT = _T('\0');
      s.ReleaseBuffer();
      return s;
   };

};


#endif // !defined(AFX_CSSFORMAT_H__20000615_9BFD_0BB5_6F1A_0080AD509054__INCLUDED_)

